package com.fengchao.api.controller.v1;

import com.fengchao.api.dao.usr.UsrWechatMapper;
import com.fengchao.model.usr.UsrWechat;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by http://my.oschina.net/wangnian on 2016/9/5.
 */
@RestController
@RequestMapping("/v1/activity")
public class ActivityController {

    @Autowired
    UsrWechatMapper usrWechatMapper;


    @RequestMapping(value = "/checkLotteryNumber", method = RequestMethod.POST)
    @ApiOperation(value = "检查是否可以抽奖", notes = "必填字段 OpenID ")
    public Integer checkLotteryNumber(@ApiParam(value = "OpenID", required = true) @RequestParam(required = true) String openId) {
        UsrWechat usrWechat = usrWechatMapper.findUnionId(openId);
        if (usrWechat == null) {
            return 0;
        }
        Integer nunber = usrWechat.getLotteryNumber();
        if (nunber != 0) {
            UsrWechat usrWechatUpdate = new UsrWechat();
            usrWechatUpdate.setId(usrWechat.getId());
            usrWechatUpdate.setLotteryNumber(usrWechat.getLotteryNumber() - 1);
            usrWechatMapper.updateByPrimaryKeySelective(usrWechatUpdate);
        }
        return nunber;
    }

}
